package com.lefevre.cms.web.action.upgrade.impl;

import com.lefevre.cms.bean.upgrade.UpgradeLog;
import com.lefevre.cms.bean.upgrade.UpgradeSystem;
import com.lefevre.cms.service.upgrade.UpgradeService;
import com.lefevre.cms.utils.JsonUtils;
import com.lefevre.cms.utils.SpringConfigTool;
import com.lefevre.cms.web.action.fileSystem.localImpl.LocalFileManage;
import com.lefevre.cms.web.action.upgrade.UpgradeManage;

import java.util.Date;

/**
 * 5.2升级到5.3版本执行程序
 */
public class Upgrade5_2to5_3 {


    /**
     * 运行
     *
     * @param upgradeId 升级Id
     */
    public static void run(String upgradeId) {
        UpgradeService upgradeService = (UpgradeService) SpringConfigTool.getContext().getBean("upgradeServiceBean");
        UpgradeManage upgradeManage = (UpgradeManage) SpringConfigTool.getContext().getBean("upgradeManage");
        LocalFileManage localFileManage = (LocalFileManage) SpringConfigTool.getContext().getBean("localFileManage");
        for (int i = 0; i < 100; i++) {
            upgradeManage.taskRunMark_delete();
            upgradeManage.taskRunMark_add(1L);
            UpgradeSystem upgradeSystem = upgradeService.findUpgradeSystemById(upgradeId);
            if (upgradeSystem == null || upgradeSystem.getRunningStatus().equals(9999)) {
                break;
            }
            if (upgradeSystem.getRunningStatus() >= 40 && upgradeSystem.getRunningStatus() < 200) {

                //更改运行状态
                upgradeService.updateRunningStatus(upgradeId, 200, JsonUtils.toJSONString(new UpgradeLog(new Date(), "升级流程结束", 1)) + ",");

            }


            if (upgradeSystem.getRunningStatus() >= 200 && upgradeSystem.getRunningStatus() < 9999) {
                //更改运行状态
                upgradeService.updateRunningStatus(upgradeId, 9999, JsonUtils.toJSONString(new UpgradeLog(new Date(), "升级完成", 1)) + ",");

            }


        }
        upgradeManage.taskRunMark_delete();
    }


}
